package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginPermission;
import cn.wolfcode.wolf2w.annotation.UserInfoResolver;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommentService travelCommentService;

    /**
     * 旅游分页
     * @param qo
     * @return
     */
    @GetMapping("/query")
    public Object query(TravelQueryObject qo){
        Page page = travelService.query(qo);

        return JsonResult.success(page);
    }


    /**
     * 旅游明细
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id){

        return JsonResult.success(travelService.get(id));
    }


    //上传图片
    @PostMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


    /**
     * 新增与编辑
     * @param travel
     * @return
     */
    @LoginPermission    //登陆才能添加
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@UserInfoResolver UserInfo user,     //使用自定义解析器获取用户信息
                               Travel travel){

        //存用户id
        travel.setUserId(user.getId());
        //存当前用户
        travel.setAuthor(user);

        String id = travelService.saveOrUpdate(travel);

        return JsonResult.success(id);
    }


    /**
     * 添加游记评论
     * @param user
     * @param travelComment
     * @return
     */
    @LoginPermission
    @PostMapping("/commentAdd")
    public Object commentAdd(@UserInfoResolver UserInfo user,     //使用自定义解析器获取用户信息
                               TravelComment travelComment){
        //将用户信息设值到评论
        BeanUtils.copyProperties(user, travelComment);
        travelComment.setUserId(user.getId());

        travelCommentService.save(travelComment);

        return JsonResult.success();
    }

    /**
     * 获取游记下的评论
     * @param travelId
     * @return
     */
    @GetMapping("/comments")
    public Object comments(String travelId){

        List<TravelComment> travelCommentList=travelCommentService.queryByTravelId(travelId);
        return JsonResult.success(travelCommentList);
    }


    @PostMapping("/test")
    public Object test(TravelComment travelComment){
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }

    @GetMapping("test2")
    public Object test2(){
        return JsonResult.defaultError();
    }


    @GetMapping
    public void test3(){
        return;
    }
}
